﻿using System.Linq;
using Loki.Utils;
using NUnit.Framework;

namespace Loki.Core.Test
{
    [TestFixture]
    public class SettingsTest : AssertionHelper
    {
        private ISettingsComponent _Service;
        private ISettingsSerializer _Serializer;

        private const string TESTPROPERTY_VALUE = "totot";

        [SetUp]
        public void SetUp()
        {
            _Service = Toolkit.Settings;
            _Serializer = new BinarySettingsSerializer();
        }

        [Test]
        public void TestConfigAddMetadata()
        {
            var L_Test = new TestSettings();
            L_Test.TestProperty = TESTPROPERTY_VALUE;
            var L_Config = _Service.Configure(L_Test).AddMetaData();

            Expect(L_Config.Properties.Count() == 1);
        }

        [Test]
        public void TestPersist1()
        {
            var L_Test = new TestSettings();
            L_Test.TestProperty = TESTPROPERTY_VALUE;
            _Service.Configure(L_Test).AddMetaData();

            _Service.PersistState(L_Test);

            Expect(_Service.GetSettings().Count > 0);
        }

        [Test]
        public void TestPersistValue()
        {
            var L_Test = new TestSettings();
            L_Test.TestProperty = TESTPROPERTY_VALUE;
            _Service.Configure(L_Test).AddMetaData();

            _Service.PersistState(L_Test);

            Expect(object.Equals(_Serializer.Deserialize(_Service.GetSettings().Values.First()), TESTPROPERTY_VALUE));
        }

        private class TestSettings
        {
            public string TestProperty
            {
                get;
                set;
            }
        }
    }
}
